package com.ruoyi.project.rm.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.rm.pojo.DataResult;
import com.ruoyi.project.rm.pojo.Task;
import com.ruoyi.project.rm.service.TaskService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("task")
@Controller
public class TaskController {

    @Autowired
    private IUserService userService;

    @Autowired
    private TaskService taskServiceImpl;

    private String prefix = "rm";

    @RequestMapping("select")
    public String selTask(){
        return prefix+"/task";
    }

    @RequestMapping("add")
    public String addTask(){
        return prefix+"/add";
    }

    @RequestMapping("show")
    public String showTask(){
        return prefix+"/show";
    }

    @RequestMapping("/task/taskInfo")
    @ResponseBody
    public DataResult<Task> taskInfo(int page, int rows, String taskName){

        User sysUser = ShiroUtils.getSysUser();
        Long userId = sysUser.getUserId();
        int n= Math.toIntExact(userId);
        DataResult<Task> dataResult=taskServiceImpl.selTaskInfo(n,taskName,page,rows);

        return dataResult;
    }

    @RequestMapping("/task/showTask")
    @ResponseBody
    public DataResult<Task> showTask(int page, int rows, String taskName){

        User sysUser = ShiroUtils.getSysUser();
        Long userId = sysUser.getUserId();
        int n= Math.toIntExact(userId);

        DataResult<Task> dataResult=taskServiceImpl.selShowTask(n,taskName,page,rows);

        return dataResult;
    }

    @RequestMapping("task/endTask")
    @ResponseBody
    public int endTask(int taskId){

        int n=taskServiceImpl.upEndTask(taskId);
        if (n>0){
            taskServiceImpl.insAddEndTask(taskId);
        }

        return n;
    }

    @RequestMapping("task/passTask")
    @ResponseBody
    public int passTask(int taskId){

        int n=taskServiceImpl.upPassTask(taskId);

        return n;
    }

    @RequestMapping("task/list")
    @ResponseBody
    public List<User> list(User user)
    {
        List<User> list = userService.selectUserList(user);
        return list;
    }

    @RequestMapping("task/addTask")
    @ResponseBody
    public int addTask(Task task)
    {

        User sysUser = ShiroUtils.getSysUser();
        Long userId = sysUser.getUserId();
        int n= Math.toIntExact(userId);
        task.setReleaseId(n);

        int i=taskServiceImpl.addTask(task);

        return i;
    }

}
